package com.bbc.base;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bbc.R;
import com.zhy.autolayout.utils.AutoUtils;
import java.util.ArrayList;
import butterknife.ButterKnife;

/**
 * adapter的基类
 */

public abstract class BaseRecAdapter<T> extends RecyclerView.Adapter {

    protected ArrayList<T> mData = new ArrayList<>();
    //普通条目的类型
    public static final int TYPE_ITEM = 0;
    //尾部加载更多的类型
    public static final int TYPE_FOOTER = 1;
    //头部条目的类型
    public static final int TYPE_HEADER = 2;

    //最后一个View,提供出去
    protected View mLoadMoreView;

    public boolean isHeader = false;


    protected TextView mTvLoadMore;
    private TextView mCommentContent;
    private TextView mLikeName;
    private CircleImageView mHeaderPhoto;
    private TextView mName;
    private TextView mTime;

    /**
     * 数据可以传空，本构造方法测试用
     *
     * @param data
     */
    public BaseRecAdapter(ArrayList<T> data) {
        if (data != null) {
            mData = data;
        }
    }

    /**
     * 真正开发使用空参构造方法，在presenter中设置数据就可以了
     */
    public BaseRecAdapter() {
    }

    //提供一个方法设置数据
    public void setData(ArrayList<T> data) {
        if (data != null) {
            mData = data;
            notifyDataSetChanged();
        }
    }

    //提供一个方法获取数据，记得做非空判断有可能为空
    public ArrayList<T> getData() {
        return mData;
    }

    /**
     * @param newData 向集合添加数据
     */
    public void addData(ArrayList<T> newData) {
        if(newData!= null  && newData.size()>0){
            mData.addAll(newData);
        }
        notifyDataSetChanged();
    }

    /**
     * 设置是否有头部
     */
    public void setHasHeader(){
        isHeader = !isHeader;
    }

    //提供一个方法获取单一数据
    public T getSingleData(int position) {
        return mData == null ? null : mData.get(position);
    }

    /**
     * @return 返回底部加载更多的View
     */
    public View getLoadMoreView(){

       return mLoadMoreView== null?null:mLoadMoreView;
    }

    /**
     * @return 返回底部加载更多的TextView
     */
    public TextView getTvLoadMore(){

        return mTvLoadMore== null?null:mTvLoadMore;
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeader && position == 0) {
            return TYPE_HEADER;
        }else if (!isHeader && position == 0) {
            return TYPE_ITEM;
        }else if(getItemCount()!= position+1){          //返回普通的条目
            return TYPE_ITEM;
        }else{
            return TYPE_FOOTER;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(TYPE_ITEM == viewType){
            View itemView = getItemView(parent);
            final BaseViewHolder baseViewHolder = getViewHolder(itemView);
            if (!itemView.hasOnClickListeners() && itemView != null) {
                itemView.setBackgroundResource(R.drawable.btn_white_selector);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClick(v, baseViewHolder.getAdapterPosition());
                    }
                });
            }
            return baseViewHolder;
        }else if (TYPE_HEADER == viewType){
            View headerView = Global.inflate(R.layout.view_move_comment_header,parent);
            HeaderViewHolder headerHolder = new HeaderViewHolder(headerView);
            return headerHolder;
        }else{
            View footView = Global.inflate(R.layout.item_footer,parent);
            FooterViewHolder footerViewHolder = new FooterViewHolder(footView);
            return footerViewHolder;
        }
    }

    //itemView的点击事件
    protected abstract void onItemClick(View v, int position);

    //为外面设置ViewHolder
    protected abstract BaseViewHolder getViewHolder(View itemView);

    //因为基类不知道怎么去加载详细条目所以，交给子类去
    protected abstract View getItemView(ViewGroup parent);

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(TYPE_FOOTER!=getItemViewType(position)){
            if(TYPE_ITEM == getItemViewType(position)){
                BaseViewHolder itemHolder= (BaseViewHolder) holder;
                if(isHeader){
                    itemHolder.setData(mData.get(holder.getLayoutPosition()-1), holder.getLayoutPosition());
                }else{
                    itemHolder.setData(mData.get(holder.getLayoutPosition()), holder.getLayoutPosition());
                }
            }else if (TYPE_HEADER == getItemViewType(0)) {
                HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
                headerHolder.setData(headerHolder.getLayoutPosition()-1);
            }
        }
    }

    @Override
    public int getItemCount() {
        //这里加1，是为了加上底部加载更多的+1;
        if (mData.size() > 0) {
            if (TYPE_HEADER == getItemViewType(0)) {
                return mData == null ? 0 : mData.size() + 2;
            }
            return mData == null ? 0 : mData.size() + 1;
        }else {
            return isHeader?1:0;
        }
    }
    public abstract class BaseViewHolder extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
            AutoUtils.autoSize(itemView);
            ButterKnife.bind(this, itemView);
        }

        /**
         * 设置数据的方法
         *
         * @param t
         * @param Position
         */
        public abstract void setData(T t, int Position);
    }
    //尾部加载更多的ViewHolder
    class FooterViewHolder extends RecyclerView.ViewHolder{
        public FooterViewHolder(View itemView) {
            super(itemView);
            mLoadMoreView = itemView;
             mTvLoadMore = (TextView) mLoadMoreView.findViewById(R.id.tv_loadMore);
        }
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder{
        protected ImageView unlike;
        protected TextView commentCount;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            unlike = (ImageView) itemView.findViewById(R.id.iv_move_comment_unlike);
            commentCount = (TextView) itemView.findViewById(R.id.tv_move_comment_count);
            mCommentContent = (TextView) itemView.findViewById(R.id.tv_comment_content);
            mLikeName = (TextView) itemView.findViewById(R.id.tv_comment_like);
            mHeaderPhoto = (CircleImageView) itemView.findViewById(R.id.iv_comment_head);
            mName = (TextView) itemView.findViewById(R.id.tv_comment_name);
            mTime = (TextView) itemView.findViewById(R.id.tv_comment_time);

            unlike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnComment != null) {
                        BaseRecAdapter.this.mOnComment.setClickLike(v);
                    }
                }
            });
        }

        /**
         * 设置数据
         * @param position
         */
        public void setData(int position) {
            if (mOnComment != null) {
                commentCount.setText(BaseRecAdapter.this.mOnComment.setComment(position) +"");
                mCommentContent.setText(BaseRecAdapter.this.mOnComment.setCommentContent(position));
                mLikeName.setText(BaseRecAdapter.this.mOnComment.setLikeName(position));
                mName.setText(BaseRecAdapter.this.mOnComment.setName(position));
                mTime.setText(BaseRecAdapter.this.mOnComment.setTime(position));
                BaseRecAdapter.this.mOnComment.setHeaderPhoto(mHeaderPhoto, position);
            }
        }
    }

    private OnComment mOnComment;
    public void setOnComment(OnComment mOnComment) {
        this.mOnComment = mOnComment;
    }
    public interface OnComment {
        int setComment(int position);
        void setClickLike(View v);
        String setCommentContent(int position);
        String setLikeName(int position);
        void setHeaderPhoto(ImageView mHeaderPhoto, int position);
        String setName(int position);
        String setTime(int position);
    }
}
